package io.bdmc.modules.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.bdmc.common.utils.*;
import io.bdmc.common.validator.Assert;
import io.bdmc.modules.bss.entity.*;
import io.bdmc.modules.bss.model.DeviceAlarmParam;
import io.bdmc.modules.bss.model.DeviceAlarmRealtimeParam;
import io.bdmc.modules.bss.model.DeviceMeasureParam;
import io.bdmc.modules.bss.model.DeviceReportParam;
import io.bdmc.modules.bss.service.*;

import io.bdmc.core.msp2000.utils.FmsDeviceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeviceStatusService {

    @Autowired
    IDeviceMachineDiskService _diskService;

    @Autowired
    IDeviceMachineFanService _fanService;

    @Autowired
    IDeviceMachineEdfaService _edfaService;

    @Autowired
    IDeviceMachineFecModuleService _fecModuleService;

    @Autowired
    IDeviceMachinePowerService _powerService;

    @Autowired
    IDeviceMachineRtService _rtService;

    @Autowired
    IDeviceMachineFspService _fspService;

    @Autowired
    IDeviceMachineRfaService _rfaService;

    @Autowired
    IDeviceMachineOswService _oswService;

    @Autowired
    IDeviceMachineOpmService _opmService;

    @Autowired
    IDeviceMachineOpmModuleService _opmModuleService;

    @Autowired
    IDeviceMachineFmsService _fmsService;

    @Autowired
    IDeviceMachineOsService _osService;

    @Autowired
    IDeviceMachineOsModuleService _osModuleService;

    @Autowired
    FmsDeviceUtil _fmsDeviceUtil;

    @Autowired
    IDeviceMachineFecService _fecService;

    @Autowired
    IDeviceDeviceService _devSvs;

    // 查询实时设备数据
    public Map<String, Object> getDeviceStatus(DeviceDevice dd) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("device", dd);
        if (dd.getParentId() > 0) {
            DeviceDevice parentDevice = _devSvs.getDeviceDetail(dd.getParentId());
            map.put("parent", parentDevice);
        }
        if (dd.getDeviceDigest().equals(Constant.DTS)) {
            return map;
        } else if (dd.getDeviceDigest().equals(Constant.MSP2000)) {
            DeviceMachineDisk disk = _diskService
                    .getOne(new QueryWrapper<DeviceMachineDisk>().eq("disk_sn", dd.getDeviceSn()).last("limit 1"));
            map.put("disk", disk);
            if (disk == null) {
                if (!dd.isFMS() && !dd.isOTDR())
                    return map;
            }
        }
        if (dd.getDeviceType().equals("FEC16") || dd.getDeviceType().equals("FEC64")
                || (dd.getDeviceDigest().equals(Constant.EU) && dd.getDeviceType().contains("FEC"))) {
            DeviceMachineFec fecData = _fecService.selectLastFecData(dd.getDeviceSn());
            if (fecData != null) {
                fecData.setFecModuleList(_fecModuleService.list(
                        new QueryWrapper<DeviceMachineFecModule>().eq("machine_fec_id", fecData.getMachineFecId())));
            }
            map.put("fec", fecData);
        } else if (dd.getDeviceType().equals("FAN")) {
            List<DeviceMachineFan> fan = _fanService.selectLastFanData(dd.getDeviceSn());
            map.put("fan", fan);
            List<DeviceMachinePower> powerList = _powerService.selectLastPowerData(dd.getDeviceSn());
            map.put("powerList", powerList);
        } else if (dd.getDeviceType().equals("EDFA") || dd.getDeviceType().equals("BA")
                || dd.getDeviceType().equals("PA")) {
            DeviceMachineEdfa edfa = _edfaService.selectLastEdfaData(dd.getDeviceSn());
            map.put("edfa", edfa);
        } else if (dd.getDeviceType().equals("RT") || dd.getDeviceType().equals("RTBA")) {
            DeviceMachineRt rt = _rtService.selectLastRtData(dd.getDeviceSn());
            map.put("rt", rt);
        } else if (dd.getDeviceType().equals("FSP6") || dd.getDeviceType().equals("FSP8")) {
            DeviceMachineFsp fsp = _fspService.selectLastFspData(dd.getDeviceSn());
            map.put("fsp", fsp);
        } else if (dd.getDeviceType().equals("OSW")) {
            DeviceMachineOsw osw = _oswService.selectLastOswData(dd.getDeviceSn());
            map.put("osw", osw);
        } else if (dd.getDeviceType().equals("OPM")) {
            DeviceMachineOpm opm = _opmService.selectLastOpmData(dd.getDeviceSn());
            if (opm != null) {
                opm.setOpmModuleList(_opmModuleService
                        .list(new QueryWrapper<DeviceMachineOpmModule>().eq("machine_opm_id", opm.getMachineOpmId())));
            }
            map.put("opm", opm);
        } else if (dd.isFMS() || dd.isOTDR()) {
            DeviceMachineFms fms = _fmsService.selectLastFmsData(dd.getDeviceSn());
            String str = dd.isOTDR() ? Constant.FMSM : Constant.FMS;
            if (fms == null) {
                DeviceDevice parent = _devSvs.getById(dd.getParentId());
                String ip = dd.isOTDR() ? dd.getIpaddr() : parent.getIpaddr();
                int port = dd.isOTDR() ? dd.getDevicePort() : parent.getDevicePort();
                fms = _fmsDeviceUtil.getFmsData(dd.getLayerRow(), ip, port, str);
                if (fms != null) {
                    String realtimeState = _fmsDeviceUtil.getFmsStatus(dd.getLayerRow(), ip, port, str);
                    String refIndex = _fmsDeviceUtil.readFMSRefIndex(dd.getLayerRow(), ip, port, str);
                    String time = _fmsDeviceUtil.readFMSMeasureTime(dd.getLayerRow(), ip, port, str);
                    fms.setState(realtimeState);
                    fms.setRefIndex(refIndex);
                    fms.setMeasureTime(Integer.parseInt(time));
                    fms.setDeviceId(dd.getDeviceId());
                    fms.setDiskSn(dd.getDeviceSn());
                    fms.insert();
                    map.put("fms", fms);
                }
            } else {
                map.put("fms", fms);
            }
        } else if (dd.getDeviceType().equals("OS")) {
            DeviceMachineOs os = _osService.selectLastOsData(dd.getDeviceSn());
            if (os != null) {
                os.setOsModuleList(_osModuleService
                        .list(new QueryWrapper<DeviceMachineOsModule>().eq("machine_os_id", os.getMachineOsId())));
            }
            map.put("os", os);
        }
        return map;
    }

    // 查询设备历史数据
    public MyMap<String, Object> getDeviceHistoryStatus(DeviceDevice device, DeviceReportParam reportParam) {
        switch (device.getDeviceType()) {
            case "FAN":
                QueryWrapper<DeviceMachineFan> w1 = new QueryWrapper<DeviceMachineFan>()
                        .eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w1.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w1.lt("create_time", reportParam.getEndTime());

                MyPage<DeviceMachineFan> page = new MyPage<>(reportParam.getCurrent(), reportParam.getSize());
                page = _fanService.selectPage(page, w1);
                return MyMap.ok(page.getRecords(), page.getTotal());
            case "BA":
            case "PA":
            case "EDFA":
                QueryWrapper<DeviceMachineEdfa> w2 = new QueryWrapper<DeviceMachineEdfa>()
                        .eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w2.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w2.lt("create_time", reportParam.getEndTime());

                MyPage<DeviceMachineEdfa> edfaPage = new MyPage<>(reportParam.getCurrent(), reportParam.getSize());
                edfaPage = _edfaService.selectPage(edfaPage, w2);
                return MyMap.ok(edfaPage.getRecords(), edfaPage.getTotal());
            case "FEC-10G":
            case "FEC-2.5G":
            case "FEC10":
            case "FEC64":
                QueryWrapper<DeviceMachineFec> w3 = new QueryWrapper<DeviceMachineFec>()
                        .eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w3.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w3.lt("create_time", reportParam.getEndTime());
                MyPage<DeviceMachineFec> fecPage = new MyPage<>(reportParam.getCurrent(), reportParam.getSize());
                fecPage = _fecService.selectPage(fecPage, w3);
                List<DeviceMachineFec> fecList = fecPage.getRecords();
                for (DeviceMachineFec deviceMachineFec : fecList) {
                    final List<DeviceMachineFecModule> machineFecModuleList = _fecModuleService
                            .list(new QueryWrapper<DeviceMachineFecModule>().eq("device_machine_id",
                                    deviceMachineFec.getMachineFecId()));
                    deviceMachineFec.setFecModuleList(machineFecModuleList);
                }
                return MyMap.ok(fecList, fecPage.getTotal());

            case "FSP6":
            case "FSP8":
                QueryWrapper<DeviceMachineFsp> w4 = new QueryWrapper<DeviceMachineFsp>()
                        .eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w4.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w4.lt("create_time", reportParam.getEndTime());

                MyPage<DeviceMachineFsp> fspPage = new MyPage<>(reportParam.getCurrent(), reportParam.getSize());
                fspPage = _fspService.selectPage(fspPage, w4);
                return MyMap.ok(fspPage.getRecords(), fspPage.getTotal());
            case "RFA":
                QueryWrapper<DeviceMachineRfa> w5 = new QueryWrapper<DeviceMachineRfa>()
                        .eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w5.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w5.lt("create_time", reportParam.getEndTime());

                MyPage<DeviceMachineRfa> rfaPage = new MyPage<>(reportParam.getCurrent(), reportParam.getSize());
                rfaPage = _rfaService.selectPage(rfaPage, w5);
                return MyMap.ok(rfaPage.getRecords(), rfaPage.getTotal());
            case "RTBA":
            case "RT":
                QueryWrapper<DeviceMachineRt> w6 = new QueryWrapper<DeviceMachineRt>()
                        .eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w6.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w6.lt("create_time", reportParam.getEndTime());

                MyPage<DeviceMachineRt> rtPage = new MyPage<>(reportParam.getCurrent(), reportParam.getSize());
                rtPage = _rtService.selectPage(rtPage, w6);
                return MyMap.ok(rtPage.getRecords(), rtPage.getTotal());
            case "OPM":
                QueryWrapper<DeviceMachineOpm> w7 = new QueryWrapper<DeviceMachineOpm>()
                        .eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w7.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w7.lt("create_time", reportParam.getEndTime());

                MyPage<DeviceMachineOpm> opmPage = new MyPage<>(reportParam.getCurrent(), reportParam.getSize());
                opmPage = _opmService.selectPage(opmPage, w7);
                List<DeviceMachineOpm> opmList = opmPage.getRecords();
                for (DeviceMachineOpm deviceMachineOpm : opmList) {
                    final List<DeviceMachineOpmModule> opmModuleList = _opmModuleService
                            .selectByMachineOpmId(deviceMachineOpm.getMachineOpmId());
                    deviceMachineOpm.setOpmModuleList(opmModuleList);
                }
                return MyMap.ok(opmPage.getRecords(), opmPage.getTotal());
            case "OS":
                QueryWrapper<DeviceMachineOs> w8 = new QueryWrapper<DeviceMachineOs>()
                        .eq("disk_sn", device.getDeviceSn()).eq("interval_save", 1);
                if (StringUtils.isNotEmpty(reportParam.getStartTime()))
                    w8.ge("create_time", reportParam.getStartTime());
                if (StringUtils.isNotEmpty(reportParam.getEndTime()))
                    w8.lt("create_time", reportParam.getEndTime());

                MyPage<DeviceMachineOs> osPage = new MyPage<>(reportParam.getCurrent(), reportParam.getSize());
                osPage = _osService.selectPage(osPage, w8);
                List<DeviceMachineOs> osList = osPage.getRecords();
                for (DeviceMachineOs machineOs : osList) {
                    final List<DeviceMachineOsModule> osModuleList = _osModuleService
                            .selectByMachineOsId(machineOs.getMachineOsId());
                    machineOs.setOsModuleList(osModuleList);
                }
                return MyMap.ok(osPage.getRecords(), osPage.getTotal());
            default:
                break;
        }
        return null;
    }
}
